package utils

import (
	"gitee.com/wengo/go-mamba"
	http "github.com/Danny-Dasilva/fhttp"
	"github.com/peterbourgon/mergemap"
	"net/url"
	"time"
)

var defaultsHeaders = map[string]interface{}{
	//"User-Agent": utils.GetRandomUa(),
	//"Accept": 			"application/json, text/plain, */*",
	"Accept": 			"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
	"Accept-Language":  "en-US,en;q=0.5",
	"Connection": "keep-alive",
	//"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4896.88 Safari/537.36",
	//"TE":"Trailers",
	"Accept-Encoding":"gzip, deflate, br",
	"Viewport-width":"1920",
	"upgrade-insecure-requests":"1",
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
}


func GetHttpClient(settings map[string]interface{}, cookies []*http.Cookie, ja3, ua string) *mamba.Client {
	// 在源码里面把使用http2关掉了
	client := mamba.NewWithRoundTripper(ja3, ua)
	client.SetDebug(true)
	client.SetDebugBodyLimit(0)
	//client.SetProxy(fmt.Sprintf("http://%s:%s@http-dynamic.xiaoxiangdaili.com:10030", "827392040156680192", "AURz6KOT"))
	headers := mergemap.Merge(defaultsHeaders, settings)

	for key, value := range headers {
		client.SetHeader(key, value.(string))
	}

	uri, _ := url.Parse("https://www.amazon.com")
	client.GetClient().Jar.SetCookies(uri, cookies)

	//client.OnBeforeRequest(func(c *mamba.Client, r *mamba.Request) error {
	//	//c.GetClient().Jar.Cookies()
	//	fmt.Println(r.Cookies)
	//	return nil
	//})
	//
	//client.OnAfterResponse(func(c *mamba.Client, r *mamba.Response) error {
	//	// Now you have access to Client and Response instance
	//	// manipulate it as per your need
	//	respCookies := r.RawResponse.Cookies()
	//	if len(respCookies) > 0 {
	//		//for _, respCookie := range respCookies {
	//		//	for _, cCookie := range c.Cookies {
	//		//		if cCookie.Name == respCookie.Name {
	//		//			cCookie.Value = respCookie.Value
	//		//		}
	//		//	}
	//		//}
	//		//fmt.Println(c.Cookies)
	//		//fmt.Println(respCookies)
	//
	//	}
	//	return nil 	// if its success otherwise return error
	//})

	client.SetRetryCount(10)
	client.SetRetryWaitTime(1 * time.Second)



	//client.SetCookies(cookies)

	//client.SetRedirectPolicy(mamba.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
	//	// Implement your logic here
	//
	//	// return nil for continue redirect otherwise return error to stop/prevent redirect
	//	fmt.Println(req, via)
	//	return nil
	//}))
	//client.SetTLSClientConfig(&tls.Config{
	//	CipherSuites: []uint16 {
	//		tls.TLS_AES_128_GCM_SHA256,
	//		tls.TLS_AES_256_GCM_SHA384,
	//		tls.TLS_CHACHA20_POLY1305_SHA256,
	//		tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
	//		tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
	//		tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
	//		tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
	//		//tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
	//		//tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
	//		tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
	//		tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
	//		tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
	//		tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
	//		tls.TLS_RSA_WITH_AES_128_CBC_SHA,
	//		tls.TLS_RSA_WITH_AES_256_CBC_SHA,
	//	},
	//
	//	MinVersion: tls.VersionTLS12,
	//	MaxVersion: tls.VersionTLS13,
	//})
	return client
}
